package system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.RegexPool;
import cn.hutool.core.util.StrUtil;
import common.model.request.SendEmailRequest;
import common.model.response.base.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import system.service.MailService;

import javax.annotation.Resource;
import javax.mail.internet.MimeMessage;
import java.util.List;
import java.util.Optional;

/**
 * 描述
 *
 * @author 米泽鹏
 * @since 2023-11-6 下午 2:38
 */
@Slf4j
@Service
public class MailServiceImpl implements MailService {

	@Resource
	private JavaMailSender javaMailSender;
	@Value("${spring.mail.username}")
	private String mailUsername;

	@Override
	public Result<?> sendEmail(SendEmailRequest request) {
		// 入参校验
		for (String to : request.getToList()) {
			if (StrUtil.isBlank(to)) {
				return Result.error("收件人邮箱不能为空");
			}
			if (!to.matches(RegexPool.EMAIL)) {
				return Result.error("收件人邮箱格式不正确");
			}
		}
		if (Boolean.TRUE.equals(request.getHtml())) {
			MimeMessage mimeMessage = javaMailSender.createMimeMessage();
			MimeMessageHelper mimeMessageHelper;
			try {
				mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
				// 发件人
				mimeMessageHelper.setFrom(mailUsername);
				// 收件人
				mimeMessageHelper.setTo(request.getToList().toArray(new String[0]));
				// 抄送人
				List<String> ccList = request.getCcList();
				if (CollectionUtil.isNotEmpty(ccList)) {
					mimeMessageHelper.setCc(ccList.toArray(new String[0]));
				}
				// 隐秘抄送人
				List<String> bccList = request.getBccList();
				if (CollectionUtil.isNotEmpty(bccList)) {
					mimeMessageHelper.setBcc(bccList.toArray(new String[0]));
				}
				// 回复人
				if (StrUtil.isNotBlank(request.getReplyTo())) {
					mimeMessageHelper.setReplyTo(request.getReplyTo());
				}
				// 邮件主题
				mimeMessageHelper.setSubject(request.getSubject());
				// 邮件内容，并设置内容 html 格式为 true
				mimeMessageHelper.setText(request.getText(), true);
				// 发送时间
				if (request.getSentDate() != null) {
					mimeMessageHelper.setSentDate(request.getSentDate());
				}
				// 发送邮件
				javaMailSender.send(mimeMessage);
			} catch (Exception e) {
				log.error("发送邮件失败", e);
			}
		} else {
			SimpleMailMessage mailMessage = new SimpleMailMessage();
			// 发件人
			mailMessage.setFrom(mailUsername);
			// 收件人
			mailMessage.setTo(request.getToList().toArray(new String[0]));
			// 抄送人
			Optional.ofNullable(request.getCcList()).ifPresent(ccList -> mailMessage.setCc(ccList.toArray(new String[0])));
			// 隐秘抄送人
			Optional.ofNullable(request.getBccList()).ifPresent(bccList -> mailMessage.setBcc(bccList.toArray(new String[0])));
			// 回复人
			mailMessage.setReplyTo(request.getReplyTo());
			// 邮件主题
			mailMessage.setSubject(request.getSubject());
			// 邮件内容
			mailMessage.setText(request.getText());
			// 发送时间
			mailMessage.setSentDate(request.getSentDate());
			// 发送邮件
			javaMailSender.send(mailMessage);
		}
		return Result.success();
	}

}
